package cn.com.taiji.oospub.wechat.common.tool;

public class BarcodeGeneration {
	public static String getDigitBarCode(String input) {
		String chr = "";
		String laststr = "";
		byte[] codeB = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 }; // 匹配字符集B
		byte[] codeC = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
				0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
				0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31,
				0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42,
				0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53,
				0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63 }; // 匹配字符集C
		int length = input.length();
		byte[] b = new byte[100];
		b[0] = 0x1b;
		b[1] = 0x64;
		b[2] = 0x02;
		b[3] = 0x1d;
		b[4] = 0x48;
		b[5] = 0x32; // 0x32打印条形码下的数字， 0x30不打印条形码下的数字
		b[6] = 0x1d;
		b[7] = 0x68;
		b[8] = 0x50; // 7F是最大的高度
		b[9] = 0x1d;
		b[10] = 0x77;
		b[11] = 0x02; // 2-6  条码宽度
		b[12] = 0x1d;
		b[13] = 0x6b;
		b[14] = 0x49; // code128
		b[15] = (byte) (length + 2); // 得出条形码长度
		b[16] = 0x7b;
		b[17] = 0x42;
		boolean result=input.matches("[0-9]+");//判断是否为纯数字
		if (length > 14 && result == true) {
			b[17] = 0x43;
			int j = 0;
			int key = 18;
			int ss = length / 2;// 初始化数组长度
			String temp = "";
			int iindex = 0;
			for (int i = 0; i < ss; i++) {
				temp = input.substring(j, j + 2);
				iindex = Integer.valueOf(temp);
				j = j + 2;
				if (iindex == 0) {
					chr = "";
					if(b[key + i - 1] == '0' && b[key + i - 2] == '0') {
						b[key + i] = codeB[0];
						b[key + i+1] = codeB[0];
						key+=1;
					}else {
						if(b[key + i-1] == 'C' && b[key + i-2] == '{' ){//判断前面的为字符集C时转换字符集B
	                        b[key + i - 2] = 0x7b;
	                        b[key + i - 1] = 0x42;
	                        b[key + i] = codeB[0];
	                        b[key + i + 1] = codeB[0];
	                        key += 1;
	                    }else{
	                        b[key + i] = 0x7b;
	                        b[key + i + 1] = 0x42;
	                        b[key + i + 2] = codeB[0];
	                        b[key + i + 3] = codeB[0];
	                        key += 3;
	                    }
					}
				} else {
					// 判断前面的为字符集B,此时要转换字符集C
					if (b[key + i - 1] == '0' && b[key + i - 2] == '0' && chr != "chr") {
						b[key + i] = 0x7b;
	                  b[key + i + 1] = 0x43;
	                  b[key + i + 2] = codeC[iindex];
	                  key += 2;
					}else {
						chr = "";
	                    b[key + i] = codeC[iindex];
	                    if(iindex == 48) chr = "chr";//判断chr(48)等于0的情况
					}
				}
			}
			int lastKey = getLastIndex(b);
			if (length % 2 > 0) {
				int lastnum = Integer.valueOf(input.substring(input.length() - 1)); // 取得字符串的最后一个数字
				if(b[lastKey] == '0' && b[lastKey-1] == '0'){//判断前面的为字符集B,此时不需要转换字符集
	                b[lastKey + 1] = codeB[lastnum];
	            }else{
	                b[lastKey + 1] = 0x7b;
	                b[lastKey + 2] = 0x42;
	                b[lastKey + 3] = codeB[lastnum];
	            }
			}
			// 得出条形码长度
			int blength = getLastIndex(b);
			int len = (blength - 15);
			b[15] = (byte) (len);
			String str = "";
			str = new String(b);
			laststr = str;
			
			String Last_two = input.substring(input.length()-2,input.length());
			int Last_two_int = 0;
			Last_two_int = Integer.valueOf(Last_two);
			if(Last_two_int > 32) {
				laststr = laststr.trim().substring(1);
			}
		} else { // 1-14位数字的条形码进来这个区间
			b[15] = (byte) (length+2);
			laststr = new String(b);
			laststr = laststr.substring(0, 18);
			laststr += input;
		}
		return laststr;
	}


	// 获取数组中最后一个不是0x0的元素的下标
	private static int getLastIndex(byte[] b) {
		if (b == null || b.length == 0) {
			return 0;
		}
		int blength = 0;
		for (int i = b.length - 1; i >= 0; i--) {
			if (b[i] != 0x0) {
				blength = i;
				break;
			}
		}
		return blength;
	}
}
